package controllers;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

import data.Data;

@SuppressWarnings("serial")
public class EditProfileServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		Utils.checkLoginFalse(request, response);

		HttpSession session = request.getSession();
		DBObject object = (DBObject) session.getAttribute("users");
		if (object != null) {
			DBCollection collection = Data.data("urlCraw");
			DBCursor cursor = collection.find(new BasicDBObject("username",
					object.get("username").toString()));

			request.setAttribute("cursor", cursor);
		}

		try {
			request.getRequestDispatcher("/edit_profile.jsp").forward(request,
					response);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		Utils.checkLoginFalse(request, response);

		HttpSession session = request.getSession();
		DBObject object = (DBObject) session.getAttribute("users");
		if (object != null) {
			DBCollection collection = Data.data("urlCraw");
			DBCursor cursor = collection.find(new BasicDBObject("username",
					object.get("username").toString()));

			request.setAttribute("cursor", cursor);
		}

		String oldpassword = request.getParameter("oldpassword");
		String newpassword = request.getParameter("newpassword");
		String renewpassword = request.getParameter("renewpassword");
		String domain = request.getParameter("domain");
		String usernamepostdomain = request.getParameter("usernamepostdomain");
		String passwordpostdomain = request.getParameter("passwordpostdomain");
		String email = request.getParameter("email");
		String passwordemail = request.getParameter("passwordemail");
		String blogid = request.getParameter("blogid");
		String type = request.getParameter("type");
		
		String error = "";
		if(!newpassword.equals(""))
		{
			if(!newpassword.equals(renewpassword))
				error = "Confirm password incorrect !";
			if(!oldpassword.equals(object.get("password")))
				error = "Old password incorrect !";
			if(error.equals(""))
			{
				object.put("password", newpassword);
			} else {
				request.setAttribute("error", error);
			}
		}
		if(error.equals(""))
		{
			//fomat domain
			domain = Utils.fomatSitemap(domain);
			
			if(type.equals("wordpress"))
			{
				object.put("usernamepostdomain", usernamepostdomain);
				object.put("passwordpostdomain", passwordpostdomain);
				object.put("domain", domain);
			}
			if(type.equals("blogger"))
			{
				object.put("email", email);
				object.put("passwordemail", passwordemail);
				object.put("blogid", blogid);
			}
			
			object.put("type", type);
			
			DBCollection collection = Data.data("users");
			collection.save(object);
			session.removeAttribute("users");
			session.setAttribute("users", object);
			request.setAttribute("success", "Edit profile success");
		}
		if (object != null) {
			DBCollection collection = Data.data("urlCraw");
			DBCursor cursor = collection.find(new BasicDBObject("username",
					object.get("username").toString()));

			request.setAttribute("cursor", cursor);
		}
		try {
			request.getRequestDispatcher("/edit_profile.jsp").forward(request,
					response);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
